<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
//    use Notifiable;
    
    protected $table = 'admin_user';
    protected $primaryKey = 'uid';
    public $incrementing = true;
    protected $guarded = [];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'nickname', 'password', 'tenant_id', 'role_id', 'last_login_at', 'person_id'
    ];
    /**
     * 需要被转换成日期的属性。
     *
     * @var array
     */
    protected $dates = ['last_login_at'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token'
    ];

    protected $appends = ['publishedCount'];
    
    // 角色
    const ROLE_ADMIN = '1';
    const ROLE_MANAGE = '2';
    const ROLE_USER = '3';
    public static $ROLE = [
        self::ROLE_ADMIN => ['name' => '系统管理员'],
        self::ROLE_MANAGE => ['name' => '管理员'],
        self::ROLE_USER => ['name' => '员工'],
    ];
    
    // 状态
    const STATUS_YES = '1';
    const STATUS_NO = '2';
    public static $STATUS = [
        self::STATUS_YES => ['name' => '可用'],
        self::STATUS_NO => ['name' => '禁用'],
    ];
    
    public function tenantName()
    {
        $tenant_name = '';
        $tenant_list = app('boss')->getTenantList();
        if($tenant_list->code == 1000)
        {
            foreach($tenant_list->data as $k=>$v)
            {
                if($this->tenant_id == $v->tenant_id)
                {
                    $tenant_name = $v->tenant_name;
                }
            }
        }
        return $tenant_name;
    }
    
    public function isSuperAdmin()
    {
        return $this->role_id == self::ROLE_ADMIN;
    }

    public static function getUsername($id)
    {
        $username = '';
        if($id > 0)
        {
            try{
                $admin = self::findOrFail($id);
                $username = $admin->username;
            } catch (\Exception $exception) {

            }

        }
        return $username;
    }


}